using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using JGSY.CMS.LowCode.Platform.Domain.Interfaces;

namespace JGSY.CMS.LowCode.Platform.Domain.Entities
{
    /// <summary>
    /// 媒体资源库实体
    /// 对应 media_files 表，用于管理系统中的所有媒体文件
    /// </summary>
    [Table("media_files")]
    public class MediaFile : ITenantEntity
    {
        /// <summary>
        /// 主键ID
        /// </summary>
        [Key]
        public long Id { get; set; }

        /// <summary>
        /// 文件名
        /// </summary>
        [Required]
        [StringLength(255)]
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 类型（图片/视频/文档）
        /// image, video, document, audio等
        /// </summary>
        [Required]
        [StringLength(50)]
        public string Type { get; set; } = string.Empty;

        /// <summary>
        /// 文件URL
        /// </summary>
        [Required]
        [StringLength(255)]
        public string Url { get; set; } = string.Empty;

        /// <summary>
        /// 文件大小（字节）
        /// </summary>
        public long Size { get; set; }

        /// <summary>
        /// 缩略图URL
        /// </summary>
        [StringLength(255)]
        public string? ThumbnailUrl { get; set; }

        /// <summary>
        /// 文件夹ID
        /// </summary>
        public long? FolderId { get; set; }

        /// <summary>
        /// 文件扩展名
        /// </summary>
        [StringLength(10)]
        public string? Extension { get; set; }

        /// <summary>
        /// MIME类型
        /// </summary>
        [StringLength(100)]
        public string? MimeType { get; set; }

        /// <summary>
        /// 文件MD5哈希值
        /// 用于文件去重和完整性校验
        /// </summary>
        [StringLength(32)]
        public string? Md5Hash { get; set; }

        /// <summary>
        /// 图片/视频宽度
        /// </summary>
        public int? Width { get; set; }

        /// <summary>
        /// 图片/视频高度
        /// </summary>
        public int? Height { get; set; }

        /// <summary>
        /// 视频/音频时长（秒）
        /// </summary>
        public int? Duration { get; set; }

        /// <summary>
        /// 文件标签（JSON格式）
        /// </summary>
        [Column(TypeName = "json")]
        public string? Tags { get; set; }

        /// <summary>
        /// 文件描述/备注
        /// </summary>
        [Column(TypeName = "text")]
        public string? Description { get; set; }

        /// <summary>
        /// 原始文件名
        /// 用户上传时的原始文件名
        /// </summary>
        [StringLength(255)]
        public string? OriginalName { get; set; }

        /// <summary>
        /// 存储路径
        /// 文件在服务器上的存储路径
        /// </summary>
        [StringLength(500)]
        public string? StoragePath { get; set; }

        /// <summary>
        /// 是否公开
        /// true=公开访问, false=需要权限验证
        /// </summary>
        public bool IsPublic { get; set; } = true;

        /// <summary>
        /// 访问次数
        /// </summary>
        public int ViewCount { get; set; } = 0;

        /// <summary>
        /// 下载次数
        /// </summary>
        public int DownloadCount { get; set; } = 0;

        /// <summary>
        /// 被引用次数
        /// 统计该文件被多少个内容项引用
        /// </summary>
        public int UsedCount { get; set; } = 0;

        /// <summary>
        /// 文件状态
        /// 0=临时文件, 1=正常, 2=回收站, 3=已删除
        /// </summary>
        public byte Status { get; set; } = 1;

        /// <summary>
        /// 最后访问时间
        /// </summary>
        public DateTime? LastAccessedAt { get; set; }

        /// <summary>
        /// 上传人
        /// </summary>
        [Required]
        public long CreatedBy { get; set; }

        /// <summary>
        /// 上传时间
        /// </summary>
        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

        /// <summary>
        /// 上传人导航属性
        /// </summary>
        [ForeignKey(nameof(CreatedBy))]
        public User? Creator { get; set; }

        /// <summary>
        /// 租户标识符
        /// </summary>
        public string TenantId { get; set; } = string.Empty;
    }
}
